package com.up4years.dataapi.controller;

import java.util.Date;
import java.util.HashMap;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.view.json.MappingJacksonJsonView;

import com.up4years.controller.base.BaseController;
import com.up4years.dataapi.entity.base.Page;
import com.up4years.dataapi.service.system.EmployeeService;
import com.up4years.utils.ParamUtils;
import com.up4years.utils.ValidUtil;

@Controller
@RequestMapping("/employee")
public class EmployeeController extends BaseController {
	@Autowired
	private EmployeeService employeeService;
	
	@RequestMapping(value="/list.htm",method=RequestMethod.POST)
	public ModelAndView list(String true_name,String mobile,String id_card,String create_time,HttpServletRequest request){
/*		if(StringUtils.isBlank(true_name)&&StringUtils.isBlank(mobile)&&StringUtils.isBlank(id_card)&&StringUtils.isBlank(create_time)){
			return new ModelAndView(new MappingJacksonJsonView(),this.getFailResult("未指定查询参数"));
		}*/
		Map<String,Object> parameters = ParamUtils.convert2Map(request);
 		Page<Map<String,Object>> page = this.getPage(request);
		page = this.employeeService.pageList(page, parameters);
		return new ModelAndView(new MappingJacksonJsonView(),this.getSuccessResult(page));
	}

	@RequestMapping("/contract.htm")
	public ModelAndView contract(String user_id){
		if(StringUtils.isBlank(user_id)){
			return new ModelAndView(new MappingJacksonJsonView(),this.getFailResult("user_id不可为空！"));
		}
		boolean result = this.employeeService.contract(user_id);
		if(result){
			return new ModelAndView(new MappingJacksonJsonView(),this.getSuccessResult());
		}
		return new ModelAndView(new MappingJacksonJsonView(),this.getFailResult("签约失败！"));
	}
	@RequestMapping("/discontract.htm")
	public ModelAndView discontract(String user_id){
		if(StringUtils.isBlank(user_id)){
			return new ModelAndView(new MappingJacksonJsonView(),this.getFailResult("user_id不可为空！"));
		}
		boolean result = this.employeeService.discontract(user_id);
		if(result){
			return new ModelAndView(new MappingJacksonJsonView(),this.getSuccessResult());
		}
		return new ModelAndView(new MappingJacksonJsonView(),this.getFailResult("解约失败！"));
	}
	@RequestMapping("/reset.htm")
	public ModelAndView reset(String user_id){
		if(StringUtils.isBlank(user_id)){
			return new ModelAndView(new MappingJacksonJsonView(),this.getFailResult("user_id不可为空！"));
		}
		boolean result = this.employeeService.reset(user_id);
		if(result){
			return new ModelAndView(new MappingJacksonJsonView(),this.getSuccessResult());
		}
		return new ModelAndView(new MappingJacksonJsonView(),this.getFailResult("重置失败！"));
	}
	@RequestMapping(value="/create.htm",method=RequestMethod.POST)
	public ModelAndView create(String code,HttpServletRequest request){
		Map<String, Object> parameters = ParamUtils.convert2Map(request);
		String mobile = (String) parameters.get("mobile");
		String id_card = (String) parameters.get("id_card");
		String true_name = (String) parameters.get("true_name");
		String sex = (String) parameters.get("sex");
		String idcard_pic1 = (String) parameters.get("idcard_pic1");
		String idcard_pic2 = (String) parameters.get("idcard_pic2");
		if(!ValidUtil.validPhone(mobile)){
			return new ModelAndView(new MappingJacksonJsonView(),this.getFailResult("mobile格式不合法！"));
		}
		if(!ValidUtil.validIdentityCard(id_card)){
			return new ModelAndView(new MappingJacksonJsonView(),this.getFailResult("id_card不可为空！"));
		}
		if(StringUtils.isBlank(true_name)){
			return new ModelAndView(new MappingJacksonJsonView(),this.getFailResult("true_name不可为空！"));
		}
		if(!"1".equals(sex)&&!"2".equals(sex)){
			return new ModelAndView(new MappingJacksonJsonView(),this.getFailResult("sex不合法！"));
		}
		if(StringUtils.isBlank(idcard_pic1)){
			return new ModelAndView(new MappingJacksonJsonView(),this.getFailResult("idcard_pic1不可为空！"));
		}
		if(StringUtils.isBlank(idcard_pic2)){
			return new ModelAndView(new MappingJacksonJsonView(),this.getFailResult("idcard_pic2不可为空！"));
		}
		//补充字段
		parameters.put("create_time", new Date());
		//物流员
		parameters.put("role", 1);
		//签约状态
		parameters.put("contract_status", 0);
		//分配工号
		parameters.put("emp_code", this.employeeService.generateEmployeeCode());
		long user_id = this.employeeService.create(parameters);
		Map<String,Object> result = new HashMap<String,Object>();
		result.put("user_id", user_id);
		if(user_id!=0l){
			return new ModelAndView(new MappingJacksonJsonView(),this.getSuccessResult(result));
		}
		return new ModelAndView(new MappingJacksonJsonView(),this.getFailResult("创建用户失败！"));
	}
}
